package com.bdqn.ch07.service;

import com.bdqn.ch07.entity.User;
import com.bdqn.ch07.repository.UserRepository;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImp implements UserService {

    @Resource
    UserRepository userRepository;

    @Override
    public User getUserByIdService(Long id) {
        return userRepository.findById(id).get();
    }

    @Override
    public User findByUserNameBiz(String usrName) {
        return userRepository.findUserByUsrName(usrName);
    }

    @Override
    public List<User> findByUserList() {
        Sort sort = new Sort(Sort.Direction.ASC, "usrId");
        return userRepository.findAll(sort);
    }

    @Override
    public Page<User> getUserListPageService(String usrName, Long roleId, int pageIndex) {
        // int page = pageIndex; //当前第几页
        int size = 2; //每页显示数据量;
        Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, size, sort);
        ;
        return userRepository.findUserPage(pageable);
    }

    public Page<User> findPageByMap(Map param, Pageable pageable) {
        return userRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if (param.get("usrName") != null) { //动态 SQL
                    predicates.add(cb.like(root.get("usrName"), "%" + param.get("usrName") + "%"));
                }
                System.out.println(param.get("roleId").toString().equals("0"));
                if (param.get("roleId") != null && !param.get("roleId").toString().equals("0")) {
                    predicates.add(cb.equal(root.get("role"), param.get("roleId")));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size(
                )]));
                // where usrName like '%usrName%'
            }
        }, pageable);
    }

    @Override
    public void saveUserService(User user) throws Exception {
        userRepository.save(user);
    }

    @CacheEvict(value = "user", keyGenerator = "keyGenerator")
    @Override
    public void delByUserIdService(Long userId) throws Exception {
        userRepository.deleteById(userId);
    }


    @Cacheable(value = "user", keyGenerator = "keyGenerator")
    @Override
    public User viewByIdService(Long userId) {
        return userRepository.findById(userId).get();
    }

/*    Specification specificationnew Specification<User>() {
        @Override
        public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            if (param.get("usrName") != null) { //动态 SQL
                predicates.add(cb.like(root.get("usrName"), "%" + param.get("usrName") + "%"));
            }
            if (param.get("roleId") != null) {
                predicates.add(cb.equal(root.get("usrRoleId"), param.get("roleId")));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size(
            )]));
        }
    }, pageable*/
}
